'use strict';

/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function (matrix, target) {
  const m = matrix.length;
  const n = matrix[0].length;
  let x = 0;
  let y = 0;

  for (let i = 0; i < n; i++) {
    if (matrix[0][i] === target) return true;
    if (matrix[0][i] < target) x++;
  }

  for (let i = 0; i < m; i++) {
    if (matrix[i][0] === target) return true;
    if (matrix[i][0] < target) y++;
  }

  if (x !== 0 && y !== 0) {
    for (let i = 0; i < y; i++)
      for (let j = 0; j < x; j++) if (matrix[i][j] === target) return true;
  }

  return false;
};

const matrix = [[1, 1]],
  target = 5;
console.log(searchMatrix(matrix, target));
